# 导入包
import websocket


# 创建事件回调函数
def on_open(ws_app):
    print('已连接')


def on_message(ws_app, message):
    print(message)


def on_error(ws_app, error):
    print(error)


def on_close(ws_app, a, b):
    print(a, b)
    print('已断开')


# 实例化一个ws客户端
url = "wss://live-ws-group1.kuaishou.com/websocket"
header = {
    "Pragma": "no-cache",
    "Origin": "https://live.kuaishou.com",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6,ko;q=0.5",
    "Sec-WebSocket-Key": "d+NBtxbsvVjpi8RxWO425Q==",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
    "Upgrade": "websocket",
    "Cache-Control": "no-cache",
    "Connection": "Upgrade",
    "Sec-WebSocket-Version": "13",
    "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits"
}
ws = websocket.WebSocketApp(
    url=url,
    header=header,
    on_message=on_message,
    on_error=on_error,
    on_open=on_open,
    on_close=on_close,
)

# 开启长连接，进行监听
ws.run_forever()

# 发送数据
# data = "08011a07088fbcbcc6e131"
# data = "08c8011adc010aac01396850742b49303339446a727a5a75496f3467644f4754633876745365587757445535595532385030453677544853513159567235432b516c6e61535052577335754b6f345374332f48333052794b704b5463364877495542716c4977614d546771657a376f59673350583035444c6b38525674776e71714b4355344b4b485853587744355459616e59306e564361754f4445794e335556497a377547674a7971346942457647314848593d120b4c72346230566a51784b633a1e486e7244546e303861744e494e466d685f31373039383133313639313732"
# ws.send(data)

# 关闭连接
ws.close()
